// shop/shop.js
Page({

    /**
     * 页面的初始数据
     */
    data: {
        carts: [],
        hasList: false,  //列表是否有数据
        totalPrice: 0, //总价
        selectAllStatus: false  // 全选
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad(options) {
        wx.request({
            hasList: true,
            url: 'http://localhost:3000/data',
            method: "GET",
            success: (res) => {
                console.log("res", res.data);
                this.setData({
                    carts: res.data
                })
            }
        })

    },

    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady() {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow() {

    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide() {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload() {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh() {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom() {

    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage() {

    },
    //计算总价
    getTotalPrice() {
        let carts = this.data.carts;  //获取购物车列表
        let total = 0;
        for (let i = 0; i < carts.length; i++) {
            if (carts[i].selected) {
                total += carts[i].num * carts[i].price;
            }
        }
        this.setData({
            carts: carts,
            totalPrice: total.toFixed(2)
        })
    },
    //选择事件
    selectList(e) {
        const index = e.currentTarget.dataset.index;
        let carts = this.data.carts;
        const selected = carts[index].selected;  //获取当前商品选中状态
        carts[index].selected = !selected;   //改变状态
        this.setData({
            carts: carts
        });
        this.getTotalPrice();   //重新获取总价
    },

    //全选
    selectAll(e) {
        let selectAllStatus = this.data.selectAllStatus;  //是否全选
        selectAllStatus = !selectAllStatus;
        let carts = this.data.carts;

        for (let i = 0; i < carts.length; i++) {
            carts[i].selected = selectAllStatus;  //改变所有商品状态
        }
        this.setData({
            selectAllStatus: selectAllStatus,
            carts: carts
        });
        this.getTotalPrice();  //重新获取总价
    },

    //加减
    addCount(e) {
        const index = e.currentTarget.dataset.index;
        let carts = this.data.carts;
        let num = carts[index].num;
        num = num + 1;
        carts[index].num = num;
        this.setData({
            carts: carts
        });
        this.getTotalPrice();
    },
    //减
    minusCount(e) {
        const index = e.currentTarget.dataset.index;
        let carts = this.data.carts;
        let num = carts[index].num;
        if (num <= 1) {
            return false;
        }
        num = num - 1;
        carts[index].num = num;
        this.setData({
            carts: carts
        });
        this.getTotalPrice();
    },

    //删除
    deletaList(e) {
        const index = e.currentTarget.dataset.index;
        let carts = this.data.carts;
        carts.splice(index, 1);   //删除
        this.setData({
            carts: carts
        });
        if (!carts.length) {
            this.setData({
                hasList: false
            });
        } else {
            this.getTotalPrice();
        }
    },

    //跳详情
    onDetail:function(e){
        const id=e.currentTarget.dataset.id;  //获取id
        console.log(id);
        //跳转页面
        wx.navigateTo({
          url: '../city/city?id='+id,
          
        })
        
    }
})